﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Cfg.MappingSchema;
using ConfOrm;
using ConfOrm.NH;
using NHibernate.Mapping.ByCode;

namespace MyStore.SqlDataAccess
{
    public class OrmMapper
    {
        private HbmMapping GetMapping()
        {
            var relationalMapper = new ObjectRelationalMapper();

            var entities = new List<Type> { typeof(MyStore.Members.Member), typeof(MyStore.Products.Product) };

            relationalMapper.TablePerClass(entities);
            relationalMapper.ManyToOne<MyStore.Products.ProductPrice, MyStore.Products.Product>();
            relationalMapper.Cascade<MyStore.Products.Product, MyStore.Products.ProductPrice>(CascadeOn.DeleteOrphans);

            var mapper = new Mapper(relationalMapper);

            mapper.Class<MyStore.Members.Member>(cm =>
            {
                cm.Table("MEMBERS");
                cm.Id(o => o.UserName, m => m.Generator(Generators.Assigned));
                cm.Id(o => o.UserName, m => m.Column("USERNAME"));
                cm.Property(o => o.Password, m => m.Column("PASSWORD"));
            });


            mapper.Class<MyStore.Products.Product>(m =>
            {
                m.Table("Products");
                m.Id(cid => cid.Code);
                m.Property(p => p.Name, map => map.Column("Name"));
            });

            mapper.Class<MyStore.Products.ProductPrice>(m =>
                {
                    m.Table(" ProductPrices");
                    m.Property(p => p.Type, mi => mi.Column(""));
                });

            return mapper.CompileMappingFor(entities);
        }


    }
}
